package com.woniuxy.meal.controller;


import com.woniuxy.meal.common.Result;
import com.woniuxy.meal.config.GetTypeByHead;
import com.woniuxy.meal.entity.User;
import com.woniuxy.meal.form.ModifyInfoForm;
import com.woniuxy.meal.form.RegisterForm;
import com.woniuxy.meal.form.UserLoginForm;
import com.woniuxy.meal.service.UserService;
import com.woniuxy.meal.util.JwtUtil;
import com.woniuxy.meal.util.RedisUtil;
import com.woniuxy.meal.util.UploadFileUtil;
import com.woniuxy.meal.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */

@Api(tags = "用户模块相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;



    @PostMapping("/register")
    @ApiOperation("注册新用户接口")
    public Result register(@Valid @RequestBody RegisterForm registerForm) throws Exception {
        userService.registerUser(registerForm);
        return Result.success(200,"新增用户成功",null);
    }




    @PostMapping("/login")
    @ApiOperation("用户登录接口(可选邮箱+密码/手机号码+验证码登录)")
    public Result login(@Valid  @RequestBody UserLoginForm userLoginForm, HttpServletRequest request, HttpServletResponse response) throws Exception{
        User user = userService.login(userLoginForm);
        HashMap<String,Object> body = new HashMap<String,Object>();
        body.put("sub",user.getId());
        String token = JwtUtil.createToken(body, 12*60);
        String refreshToken = JwtUtil.createToken(body,8*60);
        redisUtil.setString(token,refreshToken,8*60*60);
        //通过响应头将jwt响应给客户端
        response.setHeader("token", token);
        return Result.success(200,"登录成功",user);
    }


    @ApiOperation("发送验证码接口(测试固定验证码：55667)")
    @GetMapping("/sentCode")
    @ApiImplicitParam(name = "phone",value ="电话号码" )
    public Result sentRegisterCode(String phone) throws Exception {
        userService.sentCode(phone);
        return Result.success(200,"发送验证码成功",null);
    }




    @PostMapping("/upPhoto")
    @ApiOperation("用户上传头像接口")
    public Result upPhoto(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        if (!(fileType.equals("jpg")||fileType.equals("png")||fileType.equals("tif"))){
            throw new Exception("请上传图片类型的文件");
        }
        String token = WebUtil.getRequest().getHeader("token");
        Long userId = redisUtil.getUserIdByToken(token);
        userService.upPhoto(file,userId.intValue());
        return Result.success(200,"头像上传成功",null);
    }


    @PutMapping("/modify")
    @ApiOperation("修改用户信息接口")
    public Result modifyInfo(@Valid  @RequestBody ModifyInfoForm form) throws Exception {
        User user = userService.modifyInfo(form);
        return Result.success(200,"用户信息修改成功",user);
    }






}

